// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

var GameData=require("GameData");
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        RegainSpeed:600,
        isRegaining:{
            default:false,
            visible:false
        },
        mHUD:{
            default:null,
            type:cc.Node
        },
        isAchieved:{
            default:false,
            visible:false
        }
    },

    // LIFE-CYCLE CALLBACKS:
    RegainLine(){
        if(this.isRegaining)
            return;
        this.node.stopAllActions();
        var duration=Math.abs(this.node.y)/this.RegainSpeed;
        if(duration<5) duration=5;
        this.node.runAction(cc.moveTo(duration,cc.v2(0,0)).easing(cc.easeSineIn())); 
        this.isRegaining=true;
        this.isAchieved=true;
    },

    // onLoad () {},
    startLine(){
        //cc.MoveBy(duration, deltaPos, deltaY)
        this.node.stopAllActions();

        var action=cc.moveBy(5,cc.p(0,-100));
        var reaptForeverAction=cc.repeatForever(action);
        this.node.runAction(reaptForeverAction);

        this.isRegaining=false;
    },
    start () {
    },
    update (dt) {

        //show the depth
        if(!this.isRegaining){
            //set the GameData.instance.score
            GameData.instance.depth=Math.abs(Math.floor(this.node.y/10));
        }
        //cc.log(this.node.y);
        //the game end --show rest_Game and hide the start_Game
        if(this.isAchieved){
            if(this.node.y>-2){
                cc.log("show the rest UI");
                var MainUI=cc.find("Camera/MainUI",this.node)
                MainUI.active=true;
                MainUI.getChildByName("start_Game").active=false;
                MainUI.getChildByName("reset_Game").active=true;
                MainUI.getChildByName("RankButton").active=true;
                //show the Score and Depth
                cc.find("Camera/MainUI/reset_Game/ShowScore/ScoreLabel",this.node).getComponent(cc.Label).string=GameData.instance.score;
                cc.find("Camera/MainUI/reset_Game/ShowScore/Depth",this.node).getComponent(cc.Label).string="深度"+GameData.instance.depth;
                this.isAchieved=false;
            }
        }
    },

});
